package com.woniuxy.school.dietary.food.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.school.dietary.food.OssUtil;
import com.woniuxy.school.dietary.food.entity.FoodInfo;
import com.woniuxy.school.dietary.food.entity.ProductsFoodRel;
import com.woniuxy.school.dietary.food.mapper.FoodInfoMapper;
import com.woniuxy.school.dietary.food.mapper.ProductsFoodRelMapper;
import com.woniuxy.school.dietary.food.model.dto.FoodDto;
import com.woniuxy.school.dietary.food.model.from.AddFoodForm;
import com.woniuxy.school.dietary.food.model.from.FindFoodFrom;
import com.woniuxy.school.dietary.food.model.from.UpdateFoodFrom;
import com.woniuxy.school.dietary.food.service.FoodInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴彦祖
 * @since 2025年05月14日
 */
@Service
public class FoodInfoServiceImpl extends ServiceImpl<FoodInfoMapper, FoodInfo> implements FoodInfoService {
    @Resource
    private FoodInfoMapper foodInfoMapper;
    @Autowired
    private ProductsFoodRelMapper productsFoodRelMapper;
    @Resource
    private OssUtil ossUtil;
    @Override
    public FoodDto findFood(FindFoodFrom from) {
        QueryWrapper<FoodInfo> wrapper = new QueryWrapper<>();
        if (from.getFoodId() != null) {
            wrapper.eq("id", from.getFoodId());
        }
        if (from.getStoreId()!=null){
            wrapper.eq("storeId", from.getStoreId());
        }
        return BeanUtil.toBean(foodInfoMapper.selectOne(wrapper), FoodDto.class);

    }

    @Override
    public void updateFood(UpdateFoodFrom from) {
        FoodInfo foodInfo = new FoodInfo();
        foodInfo.setId(from.getFoodId());
        foodInfo.setCount(from.getCount());
        foodInfo.setFreeze(from.getFreeze());
        foodInfoMapper.updateById(foodInfo);
    }

    @Override
    public void addFood(AddFoodForm addFoodForm, String userId) throws IOException {
        String originalFilename = addFoodForm.getCoverPhoto().getOriginalFilename();

        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String filename= UUID.randomUUID()+suffix;

        String file = ossUtil.uploadFile(addFoodForm.getCoverPhoto().getInputStream(), filename, "wuhongyue117");

        FoodInfo foodInfo = new FoodInfo();
        Snowflake snowflake = new Snowflake(1, 1);
        foodInfo.setId(snowflake.nextId());
        foodInfo.setStoreId(Long.valueOf(userId));
        foodInfo.setFoodName(addFoodForm.getFoodName());
        foodInfo.setSynopsis(addFoodForm.getSynopsis());
        foodInfo.setDetailInfo(addFoodForm.getDetailInfo());
        foodInfo.setPrice(addFoodForm.getPrice());
        foodInfo.setCoverPhoto(file);
        foodInfo.setCreateUser(Long.valueOf(userId));
        foodInfo.setCreateDate(LocalDateTime.now());
        foodInfoMapper.insert(foodInfo);
    }

    @Override
    public void deleteFood(Long id, String userId) {
        LambdaQueryWrapper<FoodInfo> foodInfo = new LambdaQueryWrapper<>();
        foodInfo.eq(FoodInfo::getId, id).eq(FoodInfo::getStoreId,userId);
        if (foodInfoMapper.selectOne(foodInfo) == null) {
            throw new RuntimeException("该菜品不存在");
        }
        foodInfoMapper.delete(foodInfo);

    }

    @Override
    public Page<FoodInfo> findFoodByStoreId(Long storeId,  Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<FoodInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FoodInfo::getStoreId, storeId);
        Page<FoodInfo> page = foodInfoMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return page;

//        return foodInfoMapper.selectList(wrapper);
    }

    @Override
    public List<FoodInfo> findFoodByProductsId(Long productsId) {
        //  查询关系表
        LambdaQueryWrapper<ProductsFoodRel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductsFoodRel::getProductsId, productsId);
        List<ProductsFoodRel> productsFoodRel = productsFoodRelMapper.selectList(wrapper);
        List<FoodInfo> foodInfos=new ArrayList<>();
        //  查询菜品表
        for (ProductsFoodRel productsFoodRel1 : productsFoodRel) {
            FoodInfo foodInfo = foodInfoMapper.selectById(productsFoodRel1.getFoodId());
            foodInfos.add(foodInfo);
        }
        return foodInfos;
    }
}
